import React, {Component} from 'react';
import {
    ToastAndroid,
    Alert
} from 'react-native';

var SQLite = require('react-native-sqlite-storage');
import Constant from '../constant/constant';

export default class DbUtil extends Component {
    static open() {
        let db = SQLite.openDatabase("main.db", Constant.dbVersion, "Main Database", 20000, (() => {
        }), (() => {
            alert('打开数据库报错')
        }));
        return db;
    }

    static createTable(db) {
        let result = '0';
        //创建用户表
        db.transaction((tx) => {
            tx.executeSql('CREATE TABLE IF NOT EXISTS CURRENT_USER(' +
                'username varchar PRIMARY KEY  ,' +
                'empid varchar)'
                , [], () => {
                    DbUtil._successCB('executeSql');
                }, (error) => {
                    DbUtil._errorCB('executeSql', error);
                });

            tx.executeSql('DROP TABLE cacheDataVersion');
            tx.executeSql('CREATE TABLE IF NOT EXISTS cacheDataVersion(' +
                'id INTEGER PRIMARY KEY  AUTOINCREMENT ,' +
                'empid varchar  ,' +
                'obj varchar   ,' +
                'hashCode_ varchar)'
                , [], () => {
                    DbUtil._successCB('executeSql');
                }, (error) => {
                    DbUtil._errorCB(error);
                });

            tx.executeSql('CREATE TABLE IF NOT EXISTS BASE_USER(' +
                'username varchar PRIMARY KEY  ,' +
                'mobile varchar  )'
                , [], () => {
                    DbUtil._successCB('executeSql');
                }, (error) => {
                    DbUtil._errorCB(error);
                });


            tx.executeSql('CREATE TABLE  IF NOT EXISTS  APP_INFO(' +
                'VERSION varchar PRIMARY KEY  ,' +
                'OS varchar , ' +
                'OS_VERSION varchar , ' +
                'MOBILE_BRAND varchar ,  ' +
                'IMEI varchar  )'
                , [], () => {
                    DbUtil._successCB('executeSql');
                }, (error) => {
                    DbUtil._errorCB(error);
                });

            tx.executeSql('DROP TABLE BANNER');

            tx.executeSql('CREATE TABLE IF NOT EXISTS BANNER(' +
                'ID_ INTEGER PRIMARY KEY  AUTOINCREMENT ,' +
                'URL_ varchar,  ' +
                'SEQ_ varchar  )'

                , [], () => {
                    DbUtil._successCB('executeSql');
                }, (error) => {
                    DbUtil._errorCB(error);
                });


            tx.executeSql('DROP TABLE COURSE');

            tx.executeSql('CREATE TABLE IF NOT EXISTS COURSE(' +
                'ID_ INTEGER PRIMARY KEY   ,' +
                'AUTHOR_DESC varchar,  ' +
                'SUITABLE_FOR varchar  ,' +
                'PRICE_ double , ' +
                'COLUMN_DESC varchar  )'
                , [], () => {
                    DbUtil._successCB('executeSql');
                }, (error) => {
                    DbUtil._errorCB(error);
                });

            tx.executeSql('DROP TABLE VOICES');
            tx.executeSql('CREATE TABLE IF NOT EXISTS VOICES(' +
                'ID_ INTEGER PRIMARY KEY   ,' +
                'COURSE_ID   INTEGER,  ' +
                'FREE_ varchar  ,' +
                'IMAGE_ varchar , ' +
                'TITLE1 varchar , ' +
                'TITLE2 varchar , ' +
                'URL_ varchar  )'

                , [], () => {
                    DbUtil._successCB('executeSql');
                }, (error) => {
                    DbUtil._errorCB(error);
                });

            tx.executeSql('DROP TABLE COURSE_USER');
            tx.executeSql('CREATE TABLE IF NOT EXISTS COURSE_USER(' +
                'ID_ INTEGER PRIMARY KEY   ,' +
                'empId   varchar,  ' +
                'COURSE_ID INTEGER )'
                , [], () => {
                    DbUtil._successCB('executeSql');
                }, (error) => {
                    DbUtil._errorCB(error);
                });

            tx.executeSql('DROP TABLE SHEQUNSIWEIRI_IMAGE');
            tx.executeSql('CREATE TABLE IF NOT EXISTS SHEQUNSIWEIRI_IMAGE(' +
                'ID_ INTEGER PRIMARY KEY   ,' +
                'IMAGE_   varchar) '

                , [], () => {
                    DbUtil._successCB('executeSql');
                }, (error) => {
                    DbUtil._errorCB(error);
                });

        }, (error) => {//所有的 transaction都应该有错误的回调方法，在方法里面打印异常信息，不然你可能不会知道哪里出错了。
            DbUtil._errorCB(error);
        }, () => {
            DbUtil._successCB('createTable');
        })

    }

    static close(db) {
        if (db) {
            DbUtil._successCB('close');
            db.close();
        } else {
            console.log("SQLite not open");
        }
        db = null;
    }

    static _successCB(name) {
        // Alert.alert("SQLite "+name+" success");
    }


    static _errorCB(err) {
        console.log("SQLite " + name);
        Alert.alert('dml报错:' + err.message | error);
    }

};
